using System;
using System.Collections.Generic;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting;
using IronPython.Runtime;
using System.IO;
using Microsoft.Scripting.Hosting;
using IronPython.Modules;

namespace MyApplicationHost
{
    class MyApplication
    {
        private ScriptEngine engine;
        private ScriptRuntime env;
        private ScriptScope scope;
        //private ClrModule clr;
        ////private ScriptModule defaultModule;
        //private Scope defaultModule;

        private void Initialize()
        {
            env = ScriptRuntime.Create();
            engine = env.GetEngine(typeof(PythonContext));
            scope = env.CreateScope();
            //IScriptScope scope = env.CreateScope(); 
            //engine.Execute(scope, engine.CreateScriptSourceFromString("import clr",SourceCodeKind.Statements));
            // Import ClrModule, and make it available to the host
            // application.
            //clr = (ClrModule)engine.Import("clr");
            //clr = SystemState.Instance.ClrModule;

            ////// Make the running instance of the application available
            ////// to the default module.
            //defaultModule = ScriptDomainManager.CurrentManager.Globals.Host.DefaultModule as ScriptModule;

        }

        static void Main(string[] args)
        {
            MyApplication instance = new MyApplication();
            instance.Initialize();
            instance.Run();
            Console.Read();
        }

        private void Run()
        {
            Console.WriteLine("IronPython code accesses the host API:");
            string statements =
                "x = 'Hello'\n"          ;
            SourceUnit tempFile1;
            using (StringWriter sw = new StringWriter())
            {
                sw.WriteLine(statements);
                //tempFile1 = new SourceFileUnit(engine, "", "", sw.ToString());
                //tempFile1 = SourceUnit.CreateFileUnit(engine, "", sw.ToString());
                //tempFile1 = engine..CreateScriptSourceFromString(sw.ToString(), SourceCodeKind.File);
                engine.CreateScriptSourceFromString(sw.ToString(), SourceCodeKind.File).Execute(scope);
            }
            //tempFile1.Compile().Execute();
            
            //engine.Execute(scope, tempFile1);

            Console.WriteLine("\nHost code accesses ClrModule:");
            engine.CreateScriptSourceFromString("import clr", SourceCodeKind.Statements).Execute(scope);
            engine.CreateScriptSourceFromString("clr.AddReferenceByPartialName(\"System.Windows.Forms\")", SourceCodeKind.Statements).Execute(scope);
            
            //ClrModule.AddReferenceByPartialName("System.Windows.Forms");
            statements =
                "from System.Windows.Forms import *\n" +
                "result = MessageBox.Show('Click a button', 'Hello from IronPython', MessageBoxButtons.YesNoCancel)\n" +
                "print 'You clicked:', result";
            engine.CreateScriptSourceFromString(statements, SourceCodeKind.File).Execute(scope);

            Console.WriteLine("\nIronPython code accesses clr:");
            statements =
                "for s in clr.References:\n" +
                "  print s";
            engine.CreateScriptSourceFromString(statements, SourceCodeKind.File).Execute(scope);
            Test();
        }

        public void Greeting(string message)
        {
            Console.WriteLine("{0} from the host!", message);
        }

        public void Test()
        {
            //Print System state values to confirm proper initialization
            engine.CreateScriptSourceFromString("import sys", SourceCodeKind.Statements).Execute(scope);
            engine.CreateScriptSourceFromString("print", SourceCodeKind.InteractiveCode).Execute(scope);
            engine.CreateScriptSourceFromString("print 'sys.prefix=',sys.prefix", SourceCodeKind.InteractiveCode).Execute(scope);
            engine.CreateScriptSourceFromString("print 'sys.argv=', sys.argv", SourceCodeKind.InteractiveCode).Execute(scope);
            engine.CreateScriptSourceFromString("print 'sys.path=',sys.path", SourceCodeKind.InteractiveCode).Execute(scope);
            engine.CreateScriptSourceFromString("print", SourceCodeKind.InteractiveCode).Execute(scope);
        } 

    }
}
